import loadGif from '@/static/loading.gif';
//获取cookie
export function getCookie(key) {
	var arr = document.cookie.split("; ");
	for (var i = 0; i < arr.length; i++) {
		var arr2 = arr[i].split("=");
		if (arr2[0] == key) {
			return arr2[1];
		}
	}
}
//设置cookie
export function setCookie(key, value) {
	var oDate = new Date();
	var expiresHours = 9;
	oDate.setTime(oDate.getTime() + expiresHours * 3600);
	document.cookie = key + '=' + value + ';expries =' + oDate.toGMTString();
}
//删除cookie
export function removeCookie(key) {
	setCookie(key, -1)
}
//退出应用
export function logout() {
	console.log("执行了退出登录操作")
	var cookies = document.cookie.split(';');
	var platform = null;
	if (cookies) {
		for (var i = 0; i < cookies.length; i++) {
			if (cookies[i].indexOf("platform") > -1) {
				platform = cookies[i].split('=')[1];
				break;
			}
		}
	}
	// 在APP中打开
	if (platform && platform == "app") {
		console.log("当前是APP环境-----即将退出应用")
		var u = navigator.userAgent;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
		var isIphone = u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1;
		if (isAndroid) {
			window["liveapp"].closeWeb();
			// window.alert("安卓" + window.liveapp)
		}
		if (isiOS || isIphone) {
			window["webkit"].messageHandlers.closeWeb.postMessage({
				url: ""
			})
			// window.alert("ios" + window.webkit)
		}
	}
}
export function returnIndex() {
	window["webkit"].messageHandlers.closeWeb.postMessage({
		url: "http://game-rds.3yakj.com/webh5/"
	})
}
//判断是否是苹果用户
export function isIphone() {
	var u = navigator.userAgent;
	var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
	var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	var isIphone = u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1;
	// if (isAndroid) {
	// 	window["liveapp"].closeWeb();
	// 	// window.alert("安卓" + window.liveapp)
	// }
	if (isiOS || isIphone) {
		return true;
	} else {
		return false;
	}
}
//通知APP跳转另一个页面，也可以跳转外部浏览器
export function actionAsPhp(data) {
	let isIos=isIphone();
	let d = {
		app_open: "",
		url: ""
	};
	if (isIos) {
		d.app_open = data.ios.app_open;
		d.url = data.ios.url;
		window["webkit"].messageHandlers.actionAsPhp.postMessage(d);
	} else {
		d.app_open = data.android.app_open;
		d.url = data.android.url;
		let text = JSON.stringify(d);
		window["liveapp"].actionAsPhp(text);
	}
}
//退出H5回到APP方法
export function returnBackApp() {
	var u = navigator.userAgent;
	var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
	var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	var isIphone = u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1;
	if (isAndroid) {
		if (window.liveapp == undefined) {
			return;
		}
		window["liveapp"].closeWeb();
		// window.alert("安卓" + window.liveapp)
	}
	if (isiOS || isIphone) {
		if (window.webkit == undefined) {
			return;
		}
		window["webkit"].messageHandlers.closeWeb.postMessage({
			url: ""
		})
		//  window.alert("ios" + window.webkit)
	}
}
//时间戳转换成年月日时间
export function timestampToTime(cjsj) {
	var date = new Date(cjsj * 1000) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-'
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
	var D = date.getDate() + ' '
	var h = date.getHours() + ':'
	var m = date.getMinutes() + ':'
	var s = date.getSeconds()
	return Y + M + D;
}
//时间戳转换成月日时间
export function getMouth(cjsj) {
	var date = '';
	var lenth = cjsj.length;
	var numberData = parseInt(cjsj)
	if (lenth == 10) {
		date = new Date(numberData * 1000) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	} else if (lenth == 13) {
		date = new Date(numberData) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	}
	var Y = date.getFullYear() + '-'
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月'
	var D = date.getDate() + '日'
	var h = date.getHours() + ':'
	var m = date.getMinutes() + ':'
	var s = date.getSeconds()
	return M + D;
}
//截取地址栏token和cookie
export function getUrl(key) {
	var tokenA = getCookie('userid');
	if (tokenA) {
		return tokenA;
	}
	var url = location.search; //首先获取url
	if (url.indexOf("?") != -1) { //判断是否有参数
		var strSub = null;
		var str = url.split("?"); //根据？截取url
		var strs = str[1].split("&"); //str[1]为获取？号后的字符串，并且根据&符号进行截取，得到新的字符串数组，这个字符串数组中有n个key=value字符串
		for (var i = 0; i < strs.length; i++) { //遍历字符串数组
			strSub = strs[i].split("="); //取第i个key=value字符串，并用“=”截取获得新的字符串数组 这个数组里面第0个字符是key，第1个字符value
			if (strSub[0] == key) { //判断第0个字符与该方法的参数key是否相等，如果相等 则返回对应的值value。
				return strSub[1];
			}
		}
	}
	return "";
}
//判断是否安卓还是苹果
export function isSystem() {
	var u = navigator.userAgent;
	var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
	var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	var isIphone = u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1;
	if (isAndroid) {
		return "Android";
	}
	if (isiOS || isIphone) {
		return "Iphone";
	}
	return "";
}
//判断是否在APP内
export function isApp() {
	var cookies = document.cookie.split(';');
	var platform = null;
	if (cookies) {
		for (var i = 0; i < cookies.length; i++) {
			if (cookies[i].indexOf("platform") > -1) {
				platform = cookies[i].split('=')[1];
				break;
			}
		}
	}
	// 在APP中打开
	if (platform && platform == "app") {
		return true;
	} else {
		return false;
	}
}
//调起分享方法
export function goShare(data) {
	let system = isSystem();
	if (system == 'Android') {
		let text = JSON.stringify(data);
		window["liveapp"].openShareTip(text);
	}
	if (system == 'Iphone') {
		window["webkit"].messageHandlers.openShareTip.postMessage(data);
	}

}
//新的gif的加载 parEle参数是相对于哪个父元素的类名
export function showLoad(parEle) {
	let fEle = document.querySelector(parEle)
	let div = document.createElement('view')
	div.classList.add("load_gif")
	div.classList.add("absoluteItem")
	let img = document.createElement('img')
	div.classList.add("imgBlock")
	img.src = loadGif
	div.appendChild(img)
	fEle.appendChild(div)
}
export function hideLoad(parEle) {
	let fEle = document.querySelector(parEle)
	let div_zi = document.querySelector(".load_gif")
	fEle.removeChild(div_zi)
}
export default {
	logout,
	isIphone,
	returnBackApp,
	timestampToTime,
	getUrl,
	isSystem,
	goShare,
	isApp,
	getCookie,
	setCookie,
	removeCookie,
	getMouth,
	returnIndex,
	actionAsPhp,
	showLoad,
	hideLoad
}
